﻿using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Dapper;
using System.Data;
using MySql.Data.MySqlClient;
using DapperExtensions.Mapper;
using System.Reflection;
using DapperExtensions.Sql;

namespace SAICAPPWEB.BLL
{
    public class BaseBll<T> where T : class
    {
        protected IDatabase Db;
        public readonly string sqlconnection = ConfigurationManager.ConnectionStrings["connection"].ToString();
        public readonly string mysqlconnection = ConfigurationManager.ConnectionStrings["mysqlconnection"].ToString();

        public BaseBll()
        {
            //var connection = new SqlConnection(sqlconnection);
            //var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new SqlServerDialect());
            var connection = new MySqlConnection(mysqlconnection);
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            var sqlGenerator = new SqlGeneratorImpl(config);
            Db = new Database(connection, sqlGenerator);

        }
        public int Insert(T entity)
        {
            try
            {
                if (Db.Connection.State != ConnectionState.Open)
                    Db.Connection.Open();
                var id = Db.Insert<T>(entity);
                return id;
            }
            catch (Exception ex)
            {
                return 0;
            }
            finally
            {
                Db.Connection.Close();
            }


        }

        public T Get(int id)
        {
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();
            T resutl = Db.Get<T>(id);
            Db.Connection.Close();
            return resutl;
        }

        public bool Delete(int id)
        {
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();
            //   var query = Predicates.Field<T>(p => p.id, Operator.Eq, id);

            bool resutl = Db.Delete<T>(Db.Get<T>(id));
            Db.Connection.Close();
            return resutl;

        }

        public List<T> Query(string sql)
        {
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();
            var result = Db.Connection.Query<T>(sql);
            Db.Connection.Close();
            return (List<T>)result;

        }
        public bool Update(T entity)
        {
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();
            bool result = Db.Update<T>(entity);
            Db.Connection.Close();
            return result;

        }
        public IList<T> GetAll()
        {
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();

            var result = Db.GetList<T>().ToList();
            Db.Connection.Close();
            return result;
        }
        public IList<T> GetPage(object predicate, List<ISort> sort, int page, int resultsPerPage, out int total)
        {
            if (Db.Connection.State != ConnectionState.Open)
                Db.Connection.Open();
            total = Db.Count<T>(predicate);
            if (sort == null)
            {
                sort = new List<ISort>();
                var st = new Sort();
                st.PropertyName = "ID";
                sort.Add(st);
            }
            var result = Db.GetPage<T>(predicate, sort, page, resultsPerPage).ToList();
            Db.Connection.Close();
            return result;
        }
    }
}
